import 'package:flutter/foundation.dart';

import '../services/log_service.dart';

abstract class BaseProvider extends ChangeNotifier {
  bool _isLoading = false;
  String _error = '';
  bool _hasError = false;
  final LogService _logger = LogService();

  bool get isLoading => _isLoading;
  String get error => _error;
  bool get hasError => _hasError;
  LogService get logger => _logger;

  // 设置加载状态
  void setLoading(bool loading) {
    _isLoading = loading;
    notifyListeners();
  }

  // 设置错误信息
  void setError(String errorMessage) {
    _error = errorMessage;
    _hasError = errorMessage.isNotEmpty;
    _logger.error(errorMessage);
    notifyListeners();
  }

  // 清除错误
  void clearError() {
    _error = '';
    _hasError = false;
    notifyListeners();
  }

  // 通用的API调用错误处理
  void handleApiError(dynamic error) {
    setLoading(false);
    setError(error.toString());
  }

  // 所有子类都应该实现这个方法，确保资源正确处理
  @override
  void dispose() {
    super.dispose();
  }
}
